package com.hwadee.ems.controller;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.hwadee.ems.domain.dto.PageQuery;
import com.hwadee.ems.domain.dto.Result;
import com.hwadee.ems.domain.po.Textbook;
import com.hwadee.ems.domain.vo.TextbookVO;
import com.hwadee.ems.service.ITextbookService;


/**
 * <p>
 * 教材 前端控制器
 * </p>
 *
 * @author EnosElinsa
 * @since 2024/06/21
 */
@Slf4j
@RestController
@RequestMapping("/textbook")
public class TextbookController {

    @Resource
    private ITextbookService textbookService;

    @GetMapping("/page")
    public Result<IPage<TextbookVO>> page(PageQuery pageQuery, TextbookVO textbookVO) {
        return textbookService.page(pageQuery, textbookVO);
    }

    @PostMapping
    public Result<Long> save(@RequestBody Textbook textbook) {
        if (textbookService.save(textbook)) {
            log.info("save textbook success, id: {}", textbook.getId());
            return Result.ok(textbook.getId());
        } else {
            log.error("save textbook failed");
            return Result.error();
        }
    }

    @PutMapping
    public Result<Void> updateById(@RequestBody Textbook textbook) {
        if (textbookService.updateById(textbook)) {
            log.info("update textbook success, id: {}", textbook.getId());
            return Result.ok();
        } else {
            log.error("update textbook failed, textbook: {}", textbook);
            return Result.error();
        }
    }
    
    @DeleteMapping("/{id}")
    public Result<Void> removeById(@PathVariable Long id) {
        if (textbookService.removeById(id)) {
            log.info("remove textbook success, id: {}", id);
            return Result.ok();
        } else {
            log.error("remove textbook failed, id: {}", id);
            return Result.error();
        }
    }

}